const state = {
    globaLoadingShow: false, //显示全局loading
    withDrawType: localStorage.getItem('withDrawType') ? JSON.parse(localStorage.getItem('withDrawType')) : '', //余额还是工资的提现类型
    searchTerm: '', //当前搜索词
    searchHistory: localStorage.getItem('searchHistory') ? JSON.parse(localStorage.getItem('searchHistory')) : {}, //搜索历史
    ticketArr: localStorage.getItem('ticketArr') ? JSON.parse(localStorage.getItem('ticketArr')) : {}, //七夕活动ticketid
    way: localStorage.getItem('way') ? JSON.parse(localStorage.getItem('way')) : ''//  从哪个渠道进入
};

const getters = {
    location(state, getters) {
        return state.location;
    },
    searchTerm(state, getters) {
        return state.searchTerm;
    },
    searchType(state, getters) {
        return state.searchType;
    },
    searchHistory(state, getters) {
        return state.searchHistory;
    },
    ticketArr(state, getters) {
        return state.ticketArr;
    },
    globaLoadingShow(state, getters) {
        return state.globaLoadingShow;
    },
    withDrawType(state, getters) {
        return state.withDrawType;
    },
    way(state) {
        return state.way;
        //    进入渠道
    }
};

const mutations = {
    setLocation(state, location) {
        state.location = location;
    },
    setWay(state, way) {
        //    进入渠道
        state.way = way;
    },
    setCurrentCity(state, city) {
        state.lastCity = state.currentCity;
        localStorage.setItem(LAST_CITY_KEY, JSON.stringify(state.lastCity));
        state.currentCity = city;
        localStorage.setItem(CITY_KEY, JSON.stringify(city));
    },
    setCurrentCounty(state, county) {
        state.currentCounty = county;
        localStorage.setItem(COUNTY_KEY, JSON.stringify(county));
    },
    showGlobaLoading(state) {
        state.globaLoadingShow = true;
        // console.log(state.globaLoadingShow)
    },
    hideGlobaLoading(state) {
        state.globaLoadingShow = false;
    },
    searchTerm(state, term) {
        state.searchTerm = term;
    },
    searchType(state, type) {
        state.searchType = type;
    },
    searchHistory(state, history) {
        let system = history.system;
        let historyArray = state.searchHistory;
        // history = history.trim(); // 清除空格
        if (!historyArray[system]) {
            historyArray[system] = [];
        }
        if (historyArray[system].length > 0) {
            // 有数据的话 判断
            if (historyArray[system].indexOf(history.goodname) !== -1) {
                // 有相同的，先删除 再添加
                historyArray[system].splice(historyArray[system].indexOf(history.goodname), 1);
                historyArray[system].unshift(history.goodname);
            } else {
                // 没有相同的 添加
                historyArray[system].unshift(history.goodname);
            }
        } else {
            // 没有数据 添加
            historyArray[system].unshift(history.goodname);
        }
        if (historyArray[system].length > 10) {
            // 保留10个值
            historyArray[system].pop();
        }
        localStorage.setItem('searchHistory', JSON.stringify(historyArray));
        state.searchHistory = JSON.parse(localStorage.getItem('searchHistory'));
    },
    delSearchHistory(state, sys) {
        let historyArray = state.searchHistory;
        historyArray[sys] = [];
        localStorage.setItem('searchHistory', JSON.stringify(historyArray));
    },
    setWithDrawType(state, type) {
        state.withDrawType = type;
        localStorage.setItem('withDrawType', JSON.stringify(type));
    },
    ticketArr(state, ticket) {
        console.log('ttttttttttttttttttt', ticket);
        let system = ticket.system;
        let ticketArray = state.ticketArr;
        // ticket = ticket.trim(); // 清除空格
        if (!ticketArray[system]) {
            ticketArray[system] = [];
        }
        if (ticketArray[system].length > 0) {
            // 有数据的话 判断
            ticketArray[system].unshift(ticket.ticket);
        } else {
            // 没有数据 添加
            ticketArray[system].unshift(ticket.ticket);
        }
        if (ticketArray[system].length > 2) {
            // 保留10个值
            ticketArray[system].pop();
        }
        localStorage.setItem('ticketArr', JSON.stringify(ticketArray));
        state.ticketArr = JSON.parse(localStorage.getItem('ticketArr'));
    }
};

const actions = {
    updateLocaton({state, commit}, location) {
        commit('setLocation', location);
    },
    updateCity({state, commit}, city) {
        commit('setCurrentCity', city);
    }
};

export default {
    state,
    getters,
    actions,
    mutations
};
